//
//  LevelUpInfoMgr.m
//  SpaceStorm
//
//  Created by  mmj on 13-7-26.
//  玩家等级升级的信息管理类
//

#import "LevelUpInfoMgr.h"

@implementation LevelUpInfoMgr

@synthesize infos = _infos;

static LevelUpInfoMgr* shareDataManager = nil;

+(LevelUpInfoMgr *) sharedManager
{
    @synchronized(self)
    {
        if(shareDataManager == nil)
        {
            [[self alloc] init];
        }
    }
    return shareDataManager;
}

+(id)allocWithZone:(NSZone *)zone
{
    @synchronized(self)
    {
        if(shareDataManager == nil)
        {
            shareDataManager = [super allocWithZone:zone];
            return shareDataManager;
        }
    }
    return nil;
}

-(id) init
{
    if((self=[super init]))
    {
        self.infos = [CCArray array];
    }
    return self;
}

-(void) initConfig:(NSString*)XMLFileName
{
    tbx = [[TBXML tbxmlWithXMLFile:XMLFileName] retain];
    TBXML *root = tbx.rootXMLElement;
    if(root == nil) return;
    
    //读取信息
    TBXMLElement *element = [TBXML childElementNamed:@"node" parentElement:root];
    //循环读取节点里面的信息 节点 直到节点读取完毕
    while (element)
    {
        //将升级的节点读取成响应的 info,并且保存到数组里面
        LevelUpInfo *info = [LevelUpInfo infoWithTbxml:element];
        if(info.level != 0) //非0索引存储
        {
            [_infos addObject:info];
            CCLOG(@"当前堆栈的信息个数为%d",[_infos count]);
        }
        //读取下一个等级节点
        element = [TBXML nextSiblingNamed:@"node" searchFromElement:element];
    }
    
}

-(LevelUpInfo *) getInfoById:(int) levelUpId;
{
    LevelUpInfo *result = nil;
    for(LevelUpInfo *item in _infos)
    {
        if([item level] == levelUpId)
        {
            result = item;
        }
    }
    return result;
}

-(void) dealloc
{
    if(tbx)
    {
        [tbx release];
        tbx = nil;
    }
    if(_infos)
    {
        [_infos removeAllObjects];
        [_infos release];
        _infos = nil;
    }
    [super dealloc];
}

@end
